package com.supermap.mysql.controller;

import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.supermap.mysql.service.MsqlService;
import com.supermap.mysql.tool.MysqlEntity;
import com.supermap.mysql.tool.Table;
import com.supermap.tool.StringUtil;

/**
 * 
* @ClassName: TestController 
* @Description: 入口
* @author yuyao
* @date 2017年12月27日 上午10:33:57
 */
@RestController
public class MsqlController {
	
	@Autowired
	MsqlService testaService;
	
	//log4j2 日子管理
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());  
	
	/**
	* @Title: fastQuery 
	* @Description: 快捷查询
	* @param @param mysqlentity
	* @param @param conditionName 条件字段
	* @param @param conditionValue 字段值
	* @param @return    设定文件 
	* @return HashMap<String,Object>    返回类型 
	* @throws
	 */
    @RequestMapping(value = "/fastQuery")
	public HashMap<String, Object> fastQuery(MysqlEntity mysqlentity) {
    	
    	if(getTjpd(mysqlentity)&&!StringUtil.isNotNull(mysqlentity.getConditionValue()))return getMsg();
    	
    	String sql = testaService.getfastQuerySql(mysqlentity);
    	
		try {
			//执行查询
			if(!mysqlentity.isUpdate()) {
				if("10.230.124.131".equals(mysqlentity.getWarehouse())) {
					return testaService.querySqlDb1(sql);
				}
				if("10.230.124.132".equals(mysqlentity.getWarehouse())) {
					return testaService.querySqlDb2(sql);
				}
				if("10.230.124.133".equals(mysqlentity.getWarehouse())) {
					return testaService.querySqlDb3(sql);
				}
			}
		} catch (Exception e) {
			logger.error("查询条件异常！",e);
			return getTjMsg();
		}
		return getMsg();
	}
    
   /**
   * @Title: fastInsert 
   * @Description: 快捷添加
   * @param @param mysqlentity
   * @param @return    设定文件 
   * @return HashMap<String,Object>    返回类型 
   * @throws
    */
    @RequestMapping(value = "/fastInsert")
    public HashMap<String, Object> fastInsert(MysqlEntity mysqlentity) {
    	if(getTjpd(mysqlentity)&&!StringUtil.isNotNull(mysqlentity.getUpdateValue()))return getMsg();
    	String sql = testaService.getFastInsertSql(mysqlentity);
    	
    	try {
			if("10.230.124.131".equals(mysqlentity.getWarehouse())) {
				return testaService.updateSqlDb1(sql);
			}
			if("10.230.124.132".equals(mysqlentity.getWarehouse())) {
				return testaService.updateSqlDb2(sql);
			}
			if("10.230.124.133".equals(mysqlentity.getWarehouse())) {
				return testaService.updateSqlDb3(sql);
			}
		} catch (Exception e) {
			logger.error("新增条件异常！",e);
			return getTjMsg();
		}
    	return getMsg();
	}
    
	
    /**
    * @Title: fastUpdate 
    * @Description:快捷更新
    * @param @param mysqlentity
    * @param @param condition
    * @param @return    设定文件 
    * @return HashMap<String,Object>    返回类型 
    * @throws
     */
	@RequestMapping(value = "/fastUpdate")
	public HashMap<String, Object> fastUpdate(MysqlEntity mysqlentity) {
		
		if(getTjpd(mysqlentity)&&!StringUtil.isNotNull(mysqlentity.getConditionValue())&&
				StringUtil.isNotNull(mysqlentity.getUpdateValue()))return getMsg();
		
		String sql = testaService.getFastUpdateSql(mysqlentity);
		
		try {
			if("10.230.124.131".equals(mysqlentity.getWarehouse())) {
				return testaService.updateSqlDb1(sql);
			}
			if("10.230.124.132".equals(mysqlentity.getWarehouse())) {
				return testaService.updateSqlDb2(sql);
			}
			if("10.230.124.133".equals(mysqlentity.getWarehouse())) {
				return testaService.updateSqlDb3(sql);
			}
		} catch (Exception e) {
			logger.error("查询条件异常！",e);
			return getTjMsg();
		}
		return getMsg();
	}
	
	/**
	* @Title: findTable 
	* @Description: 查询列表
	* @param @param table
	* @param @param mysqlentity
	* @param @return    设定文件 
	* @return Table    返回类型 
	* @throws
	 */
	@RequestMapping(value = "/findTable")
	public Table findTable(Table table,MysqlEntity mysqlentity) {
		if(getTjpd(mysqlentity)&&!StringUtil.isNotNull(mysqlentity.getConditionValue()))return table;
    	String sql = testaService.findTableListSql(mysqlentity, table);
    	String sqlCount = testaService.findTableCount(mysqlentity, table);
		try {
			//执行查询
			if(!mysqlentity.isUpdate()) {
				if("10.230.124.131".equals(mysqlentity.getWarehouse())) {
					 HashMap<String, Object> dataList = testaService.querySqlDb1(sql);
					 HashMap<String, Object> dataCount = testaService.querySqlDb1(sqlCount);
					return getTable(table, dataList, dataCount);
				}
				if("10.230.124.132".equals(mysqlentity.getWarehouse())) {
					HashMap<String, Object> dataList = testaService.querySqlDb1(sql);
					 HashMap<String, Object> dataCount = testaService.querySqlDb2(sqlCount);
					return getTable(table, dataList, dataCount);
				}
				if("10.230.124.133".equals(mysqlentity.getWarehouse())) {
					HashMap<String, Object> dataList = testaService.querySqlDb1(sql);
					 HashMap<String, Object> dataCount = testaService.querySqlDb3(sqlCount);
					return getTable(table, dataList, dataCount);
				}
			}
		} catch (Exception e) {
			logger.error("查询条件异常！",e);
			table.setMsg("查询异常！");
			return table;
		}
		table.setMsg("OK");
		return table;
	}
	
	/**
	* @Title: queryMsql 
	* @Description: mysql sql入口
	* @param @param mysqlentity
	* @param @return    设定文件 
	* @return HashMap<String,Object>    返回类型 
	* @throws
	 */
	@RequestMapping(value = "/mysql")
	public HashMap<String, Object> Msql(MysqlEntity mysqlentity) {
		
		if(getTjpd(mysqlentity)&&!StringUtil.isNotNull(mysqlentity.getSql()))return getMsg();
		
		try {
			if(mysqlentity.getSql().trim().toUpperCase().startsWith("select".toUpperCase())) {
				if("10.230.124.131".equals(mysqlentity.getWarehouse())) {
					return testaService.querySqlDb1(mysqlentity.getSql());
				}
				if("10.230.124.132".equals(mysqlentity.getWarehouse())) {
					return testaService.querySqlDb2(mysqlentity.getSql());
				}
				if("10.230.124.133".equals(mysqlentity.getWarehouse())) {
					return testaService.querySqlDb3(mysqlentity.getSql());
				}
			} else {
				//执行更新
				if("10.230.124.131".equals(mysqlentity.getWarehouse())) {
					return testaService.updateSqlDb1(mysqlentity.getSql());
				}
				if("10.230.124.132".equals(mysqlentity.getWarehouse())) {
					return testaService.updateSqlDb2(mysqlentity.getSql());
				}
				if("10.230.124.133".equals(mysqlentity.getWarehouse())) {
					return testaService.updateSqlDb3(mysqlentity.getSql());
				}
			} 
		} catch (Exception e) {
			logger.error("查询条件异常！",e);
			return getTjMsg();
		}
		return getMsg();
	}

	
	public HashMap<String, Object> getMsg(){
		 HashMap<String, Object> map = new HashMap<String, Object>();
		 map.put("code", "1");
		 map.put("msg", "操作的数据库不存在！当前支持如下数据库：10.230.124.131,10.230.124.132,10.230.124.133;请核对数据库名称！");
		return map;
	}
	
	public HashMap<String, Object> getTjMsg(){
		 HashMap<String, Object> map = new HashMap<String, Object>();
		 map.put("code", "1");
		 map.put("msg", "sql条件异常！请检查执行条件！");
		return map;
	}
	/**
	* @Title: getTjpd 
	* @Description: 判断基本条件是否为空
	* @param @param mysqlentity
	* @param @return    设定文件 
	* @return Boolean    返回类型 
	* @throws
	 */
	public Boolean getTjpd(MysqlEntity mysqlentity){
		if(StringUtil.isNotNull(mysqlentity.getWarehouse())&&StringUtil.isNotNull(mysqlentity.getTableName()))return false;
		return true;
	}
	
	@SuppressWarnings("unchecked")
	public Table getTable(Table table,HashMap<String, Object> dataList,HashMap<String, Object> dataCount) {
		 table.setData((List<HashMap<String, Object>>)dataList.get("data"));
		 List<HashMap<String, Object>> list = (List<HashMap<String, Object>>) dataCount.get("data");
		  Object dsd = list.get(0).get("count");
		 table.setCount(Integer.parseInt(dsd.toString()));
		 return table;
	}
	
}
